package com.pinxiang.framework.weather.power.controller;

import com.alibaba.fastjson.JSON;
import com.pinxiang.framework.weather.power.model.MtIceData;
import com.pinxiang.framework.weather.power.service.IMtIceDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/ice")
public class IceDataController {
    @Autowired
    private IMtIceDataService mtIceDataService;

    @RequestMapping(value = "/dataGrid")
    @ResponseBody
    public String query(Integer pageSize, Integer pageNumber, String startTime, String endTime, String deviceCode, String equnum, HttpServletRequest request,
                        HttpServletResponse response) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        int arg1 = (pageNumber - 1) * pageSize + 1;
        int arg2 = pageSize;
        parameters.put("startTime", startTime);
        parameters.put("endTime", endTime);
        parameters.put("deviceCode", deviceCode);
        parameters.put("equnum", equnum);
        parameters.put("arg1", arg1);
        parameters.put("arg2", arg2);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<MtIceData> historyDatas = mtIceDataService.findAll(parameters);
        int total = mtIceDataService.selectCountByParams(parameters);
        resultMap.put("total", total);
        resultMap.put("rows", historyDatas);
        String json = JSON.toJSONString(resultMap);
        return json;
    }

    @RequestMapping(value = "/loadIce")
    @ResponseBody
    public String loadIce(String startTime, String endTime, String deviceCode, String flag, HttpServletRequest request,
                          HttpServletResponse response) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("startTime", startTime);
        parameters.put("endTime", endTime);
        parameters.put("deviceCode", deviceCode);
        parameters.put("flag", flag);
        String result = "";
        List<Object> list = new ArrayList<Object>();
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        List<MtIceData> selectpieList = mtIceDataService.selectForpieDisplay(parameters);
        String[] legend_pie = new String[selectpieList.size()];
        Object[] data_pie = new Object[selectpieList.size()];
        for (int i = 0; i < selectpieList.size(); i++) {
            MtIceData ice = selectpieList.get(i);
            legend_pie[i] = ice.getName();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", ice.getName());
            map.put("value", ice.getValue());
            data_pie[i] = map;
        }
        list.add(legend_pie);
        list.add(data_pie);

        List<MtIceData> selectX = mtIceDataService.selectForX(parameters);
        String[] x = new String[selectX.size()];
        for (int i = 0; i < selectX.size(); i++) {
            x[i] = selectX.get(i).getName().substring(5);
        }
        list.add(x);
        String[] legend_bar = {"*", "V", "~", "~V",};
        String[] x_tar;
        list.add(legend_bar);
        for (int j = 0; j < legend_bar.length; j++) {
            parameters.put("species", legend_bar[j]);
            List<MtIceData> selecttarList = mtIceDataService.selectFortarDisplay(parameters);
            int[] data_tar = new int[selecttarList.size()];

            for (int m = 0; m < selecttarList.size(); m++) {
                MtIceData tar = selecttarList.get(m);
                data_tar[m] = tar.getValue();

            }
            list.add(data_tar);
        }


        result = JSON.toJSONString(list);

        return result;
    }

    @RequestMapping(value = "/loadIceDE")
    @ResponseBody
    public String loadIceDE(String startTime, String endTime, String deviceCode, String flag, HttpServletRequest request,
                            HttpServletResponse response) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("startTime", startTime);
        parameters.put("endTime", endTime);
        parameters.put("deviceCode", deviceCode);
        parameters.put("flag", flag);
        String result = "";
        List<Object> list = new ArrayList<Object>();
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        List<MtIceData> selectpieList = mtIceDataService.selectForDEDisplay(parameters);
        String[] legend_pie = new String[selectpieList.size()];
        Object[] data_pie = new Object[selectpieList.size()];
        for (int i = 0; i < selectpieList.size(); i++) {
            MtIceData ice = selectpieList.get(i);
            legend_pie[i] = ice.getDatetime();
            data_pie[i] = ice.getIceWeihgh();
        }
        list.add(legend_pie);
        list.add(data_pie);

        result = JSON.toJSONString(list);

        return result;
    }
}
